﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Collections;
using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.Model;

namespace Eis51.Web.dataPage
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProductDownload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            StringBuilder sb = new StringBuilder();

            LoadNews(ref sb);

            context.Response.Write("[ " + sb.ToString() + "]");
        }

        public class ShowModel
        {

            public int Seqno { get; set; }

            public string Title { get; set; }

            public string Url { get; set; }
            public int TypeID { get; set; }
            public string TypeName { get; set; }

        }


        private void LoadNews(ref StringBuilder hoemData)
        {
            string scondition = string.Format(" Licd={0} and Isfirst>0 ", AppContext.LCID);
            scondition += " order by type desc ";
            IList list = ProductsBLL.GetInstance().Search(scondition);
            ShowModel showModel = null;
            Products model = null;
            string tmpStr = string.Empty;

            IList typeList = ProductsTypeBLL.GetTypeList(AppContext.GetCurrentName(), AppContext.LCID);

            if (null != list && list.Count > 0)
            {
                foreach (var et in list)
                {
                    model = (Products)et;
                    if (string.Empty.Equals(model.Title))
                    {
                        continue;
                    }
                    string fileUrl = FileListBLL.ShowDownloadFile(model.ImgBigPath);
                    string typeName = string.Empty;
                    ProductsType etType=null;
                    if (null != typeList && typeList.Count>0)
                    for(int i=0;i<typeList.Count;i++)
                    {
                        etType = (ProductsType)typeList[i];
                        if (etType.Seqno == model.Type) {
                            typeName = etType.Title;
                            break;
                        }
                    }
                    showModel = new ShowModel { Seqno = model.Seqno, Title = model.Title, Url = fileUrl, TypeID = model.Type, TypeName = typeName };
                    tmpStr = Eis51.Web.common.common.JavaScriptSerializerString(showModel);
                    hoemData.Append(",");
                    hoemData.Append(tmpStr);
                }
            }
        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
